<?php
namespace Home\Controller;
use Home\Controller\CommonController;
class QuanziController extends CommonController {

    /**
      +----------------------------------------------------------
     * 首页
     *
      +----------------------------------------------------------
     */
        private $count1;
    protected $comments_model;



    function __construct() {
        parent::__construct();
        $this->comments_model=D("Comments");

    }



  public function fabu(){

       if (IS_POST) {
       
           $data['title']=I('post.title');
           $data['img']=implode(",",$_POST['photos']);
           $data['content']=I('post.content');
           $data['uid']=$_SESSION['userid'];
           $data['time']=time();
          if(M('quanzi')->add($data)){
                 $this->redirect(U('Quanzi/index'));

          }else{
            $this->error('发布失败');
          }

         }


    
  }

    public function index() {
         // $gg= M('quanzi')->select();
         //  foreach ($gg as $key => $value) {
         //   print_r($value[img]);
         //      }

             $count = M('quanzi')->where(array("pid"=>0))->count();

  $Page =new \Think\Page($count,10);
    $Page -> setConfig('first','首页');
    $Page -> setConfig('last','末页');
    $Page -> setConfig('prev','上一页');
    $Page -> setConfig('next','下一页');
    // if($count < $max_count){
    //   $one = "1";

    // }
    $Page -> setConfig('theme',' <div style="margin-top:2px">  %FIRST%  %UP_PAGE%    %LINK_PAGE%  %DOWN_PAGE%   %END% </div> ');
    $show = $Page->show();


     

       // $quanzi=M('quanzi')->alias("a")->field("a.id,a.title,a.content,a.time,a.img,a.uid,b.user as username")->join("zyb_user b on b.id = a.uid")->order("a.id desc")->limit($Page->firstRow, $Page->listRows)->select();
 
       //  foreach ($quanzi as &$value) {
       //      $value['img']=(explode(",",$value['img']));
       //  }
            $this->count1 = 0; 
               $quanzi = $this->quanzi(0,$quanzi,$Page->firstRow,$Page->listRows);                  
               $this->assign("page",$show);
               $this->assign('quanzi',$quanzi);

         

  // $quanzi_status = 0;
  //       $result = M('shoucang')->where(array("userid"=>$_SESSION["userid"]))->find();
  //       if($result){
 
  //         $quanzi_shoucang = explode(',',$result['quanzi']);

  //         $is_has = false;
  //         foreach ($quanzi_shoucang as $key => $value) {
       
            
  //           if($value){
  //             $is_has = true;
  //             break;
  //           }
  //         }
  //       }
  //       if($is_has){
  //         $quanzi_status = 1;

  //       }


  //  $this->assign("quanzi_status",$quanzi_status);
 			

            $this->display();
 		
        
    }

    public function quanzi($pid,$str,$firstRow,$listRows){
 if ($pid == 0) {
            $quanzi = M('quanzi')->alias("a")->field("a.id,a.title,a.content,a.time,a.img,a.uid,a.pid,a.zan,b.user as username,b.touxiang,' ' as shoucang")->join("zyb_user b on b.id = a.uid")->where(array("a.pid" => $pid))->order("a.id desc")->limit($firstRow, $listRows)->select();
        } else {
            $quanzi = M('quanzi')->alias("a")->field("a.id,a.title,a.content,a.time,a.img,a.uid,a.pid,b.user as username, b.touxiang,' ' as shoucang")->join("zyb_user b on b.id = a.uid")->where(array("a.pid" => $pid))->order("a.id desc")->select();
        }

        if ($quanzi) {
   
               $result = M('shoucang')->where(array("userid"=>$_SESSION["userid"]))->find();
               if($result){
                 $quanzi_shoucang = explode(',',$result['quanzi']);
               }
               if(!empty($quanzi_shoucang)){
                  foreach ($quanzi as $key => $value) {
                      
                        foreach ($quanzi_shoucang as $key1 => $value1) {
                          if($value["id"]==$value1){
                            $quanzi[$key]["shoucang"] = 1;
                          }
                        }
                  }
                }
            $this->count1 = $this->count1 + 1;
            foreach ($quanzi as $key => $value) {
                // $value         date()$value['time']
                if ($value["pid"] == 0) {
                    // $str .= "<div style='margin-left:{$margin_left}px;'>{$value['id']}</div>";
                    $str .= '<div class="groupList">';
                    $str .= '  <div class="li">';
                    $str .= '    <div class="user fl">';
                    $str .= '      <img style="width:80px; height:58px;" src="Uploads/' . $value['touxiang'] . '"><p>' . $value["username"] . '</p>';
                    $str .= '    </div>';
                    $str .= '   <div class="other fr">';
                    $str .= '     <div class="p1">';

                    $str .= '       <h1>
                                       <a href="javascript:;" onclick="redirect(' . $value["id"] . ')">' . $value["title"] . '</a>';
           
                    if($value["shoucang"]==1){
                      $str .= '           <a class="favour checked" href="javascript:;" data-id="'.$value["id"].'"></a>
                                    </h1>';
                    }else{
                      $str .= '           <a class="favour " href="javascript:;" data-id="'.$value["id"].'"></a>
                                    </h1>';
                    }
                    
                    $str .= '         <p><a href="javascript:;" onclick="redirect(' . $value["id"] . ')">' . $value["content"] . '</a></p>';
                    $str .= '      <div class="imgs">';
                    $imgs = explode(",", $value['img']);
                    foreach ($imgs as $key1 => $value1) {
                        $str .= '     <img src="Uploads/' . $value1 . '" onclick="redirect(' . $value["id"] . ')">';
                    }
                    $str .= '      </div>';

                    $str .= '     <div class="btn"><span class="fl">' . date('Y-m-d H:i', $value["time"]) . '</span>';
                    $zan = empty($value["zan"]) ? 0 : $value["zan"];
                    $str .= '       <a href="javascript:;" onclick="zan(' . $value["id"] . ')"><img src="App/Home/View/Public/images/group_icon02.png">赞（' . $zan . '）</a>';
                    // $str .= '      <a><img src="App/Home/View/Public/images/group_icon03.png">评论（5）</a>';
                    // $str .= '      <a href="#"><img src="App/Home/View/Public/images/group_icon04.png">转发</a>';
                    $str .= '     </div></div>';

                    // $margin_left1 = $margin_left + 5;
                    $str = $this->quanzi($value["id"], $str);
                    // $str .= '<div class="submit"><input type="text" placeholder="发表评论"><button type="submit">发送</button>';
                    // $str .= '</div>';

                    $str .= '<div class="cornor"><img src="App/Home/View/Public/images/cornor.png"></div>';
                    $str .= '   </div><div class="clear"></div></div>';
                    $str .= '   </div>';
                }

            }

        }
        return $str;
        
    }


    
    public function tupian(){


       


 if (!empty($_FILES)) {
            //图片上传设置
             $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     553145728 ;// 设置附件上传大小
                $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
                $upload->savePath  =     ''; // 设置附件上传（子）目录
                // 上传文件
       
            $images = $upload->upload();
    
            //判断是否有图
            if($images){
                $info=$images['Filedata']['savepath'].$images['Filedata']['savename'];
  


                //返回文件地址和名给JS作回调用
                echo $info;
         
                 
            }
            else{
                $this->error($upload->getError());//获取失败信息
            }
        }







    }
   public function spid(){

if(empty($_SESSION['userid'])){
    echo 3;
}else{
    $pid=I('post.pid');
$content=I('post.content');

if (M('quanzi')->add(array('content'=>$content,'pid'=>$pid,'uid'=>$_SESSION['userid'],'time'=>time()))){

     echo 1;

}else{
    echo 0;
}
}


   }


      public function zan(){
        $id=I('post.id');
        if ( M('quanzi')->where(array("id"=>$id))->setInc('zan','1')) {
            echo 1;
         }
        
    }





    /*
     *  圈子详情
     *  @author xiejg
     *  date:2017.3.28
     */
    public function details(){
        $qz_id = I('get.id');
        $list = M('Quanzi')->where(array('id'=> $qz_id))->find();
 
     
                 $m=M('user')->where('id='.$list['uid'])->find();
                 $list['xingming']=$m['user'];
                    $list['touxiang']=$m['touxiang'];
   
        foreach ($list as $k=>$v){
            $list['imgsrc'] =explode(",", $list['img']);
        }
            

        $comments = $this->CommentList($pid = 0, $commentList = array(), $spac = 0,$qz_id);


        foreach ($comments as $key => &$value) {
                $m=M('user')->where('id='.$value['uid'])->find();

                $value['touxiang']=$m['touxiang'];
        }
        $comments_id = I('get.comments_id');
        if(!empty($comments_id)){
           M('comments')->where(array('id'=>$comments_id))->setField('status',2);
        }
     
        $this->assign('commentList',$comments);
        $this->assign('ace',$list['imgsrc']);
        $this->assign('list',$list);
        // $this->assign('qz_id',$qz_id);
        $this->display();
    }
    //提交评论
    public function comment_post()
    {
//        file_put_contents("log.txt",  var_export($_POST, true), FILE_APPEND);
        if (IS_POST) {
            if ($this->comments_model->create()) {
                $result = $this->comments_model->add();
                if ($result !== false) {
                    //评论计数
                    $this->ajaxReturn(array("status"=>1,"info"=>"评论成功"));
                } else {
                    $this->ajaxReturn(array("status"=>0,"info"=>"评论成功"));
                }

            }
        }
    }

    //评论列表
    function CommentList($pid = 0, &$commentList = array(), $spac = 0,$qz_id) {
        static $i = 0;
        $spac = $spac + 1; //初始为1级评论
        $List =$this->comments_model
                    ->field('id,createtime,full_name,content,parentid,uid')
                    ->where(array('parentid' => $pid,'qz_id'=>$qz_id))
                    ->order("id DESC")
                    ->select();
        //dump($List);
        foreach ($List as $k => $v) {
            $commentList[$i]['level'] = $spac; //评论层级
            $commentList[$i]['full_name'] = $v['full_name'];
            $commentList[$i]['id'] = $v['id'];
            $commentList[$i]['parentid'] = $v['parentid']; //此条评论的父id
            $commentList[$i]['content'] = $v['content'];
            $commentList[$i]['createtime'] = $v['createtime'];
            $commentList[$i]['uid'] = $v['uid'];
            // $commentList[$i]['pauthor']=$pautor;
            $i++;
            $this->CommentList($v['id'], $commentList, $spac,$qz_id);
        }
        return $commentList;
    }



  public function search(){

          $list=M('zlist')->select();
          
          $huxing=M('huxing')->order('id asc')->select();
          
          $fengge = I("get.fengge");
       $huxing1 = I("get.huxing");
       $where = "";

       if(!empty($fengge)){
          $data = M('zlist')->where("id = $fengge")->find();
          if(empty($where)){
            $where .= "a.fengge = '".$data['type_name']."'";
          }else{
            $where .= " and a.fengge = '".$data['type_name']."'";
          }
          
           
       }

       if(!empty($huxing1)){
          $data1 = M('huxing')->where("id=$huxing1")->find();
          if(empty($where)){
            $where .= "a.huxing = '".$data1['huxing']."'";
          }else{
            $where .= " and a.huxing = '".$data1['huxing']."'";
          }
       }
       
       $title=I('post.title');
         $map['a.name'] =array('like',"%$title%");
          $map['a.fengge'] =array('like',"%$title%");
          $map['a.huxing'] =array('like',"%$title%");
          $map['_logic'] = 'or';
    $shangpin=M('shangpin')->alias("a")->field("a.id,a.name,a.fengge,a.huxing,a.area,a.price,a.intro,a.img,a.userid,b.user as username")->join("zyb_user b on b.id = a.userid")->where($map)->order("a.id desc")->select();
         
            $this->assign("fengge",$fengge);
      $this->assign('list',$list);
      $this->assign('huxing',$huxing);
      $this->assign('huxing1',$huxing1);
            $this->assign('shangpin',$shangpin);
            $this->display();
        
    }  
    




}
 
?>